import requests
from lxml import etree
import re
import json

url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1762174732694_112&jsoncallback=jsonp113&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'

headers = {
    "accept": "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01",
    "accept-encoding": "gzip, deflate, br, zstd",
    "accept-language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7",
    "bx-v": "2.5.31",
    "cache-control": "no-cache",
    "cookie": "t=50ef8feaf7fab29f890251f00e219b26; cookie2=1af38c70b34a9d7fdb01393ba7288ab1; v=0; _tb_token_=5b33ebb43e4a3; cna=w5SPIRQnvBMCAQAAAABBnlpx; xlly_s=1; isg=BMPDN21triIm8GJWQxPlIpvLUodtOFd6aPfzKvWgkCKZtOLWfQoXyDliLkT6XK9y",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://dianying.taobao.com/",
    "sec-ch-ua": "\"Chromium\";v=\"142\", \"Google Chrome\";v=\"142\", \"Not_A Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36",
    "x-requested-with": "XMLHttpRequest"
}
res = requests.get(url,headers=headers)
# print(res.text)

# 提取 JSON 字符串
json_str = re.search(r'jsonp113\((.*)\);$', res.text).group(1)

# 解析为字典
data = json.loads(json_str)
ls = []
for letter in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
    for item in data['returnValue'].get(letter, []):
# for item in data['returnValue'].get(letter, [])这句话
#把当前字母 letter（如 'A'、'B'…）当键去查；
# 若这个字母没有对应的城市列表，就返回空列表 []，避免 KeyError，后面的 for 循环也就自然跳过
#         print(item['regionName'])
        ls.append(item['regionName'])
print(ls)
